package com.jcfk.eam.service.base;

import com.jcfk.common.business.service.BaseService;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.RetResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jcfk.eam.dao.base.EamSparePartDao;
import com.jcfk.eam.domain.dto.base.*;
import com.jcfk.eam.domain.dto.equ.EquSparePartsTypeDTO;
import com.jcfk.eam.domain.dto.main.EamAssetInfoDTO;
import com.jcfk.eam.domain.po.base.EamRefAssetPart;
import com.jcfk.eam.domain.po.base.EamSparePart;
import com.jcfk.eam.domain.query.base.EamSparePartQuery;
import com.jcfk.eam.service.equ.EquSparePartsTypeService;
import com.jcfk.eam.service.main.EamAssetInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;
import com.jcfk.common.core.utils.BeanUtils;
import cn.hutool.core.util.StrUtil;
import com.jcfk.common.security.utils.SecurityUtils;
import com.alibaba.druid.util.StringUtils;

import java.util.List;

import static com.jcfk.common.core.utils.BeanUtils.convertList;

import java.util.Date;

/**
 * 备件
 *
 * @author awei
 * @email 522891237@gqq.com
 * @date 2024-08-14
 */
@Service
public class EamSparePartService extends BaseService<EamSparePartDao, EamSparePart> {

    @Autowired
    private EamMakerService eamMakerService;

    @Autowired
    private EamAssetBrandService eamAssetBrandService;

    @Autowired
    private EamAssetTypeService eamAssetTypeService;

    @Autowired
    private EamRefAssetPartService eamRefAssetPartService;
    @Autowired
    private EamAssetInfoService eamAssetInfoService;
    @Autowired
    private EquSparePartsTypeService eamSparePartKindService;

    /**
     * EamSparePart分页查询
     *
     * @param page  分页设置
     * @param query 查询条件
     * @return 分页查询结果
     */
    public DataPage<EamSparePartDTO> queryByPage(DataPage<EamSparePartDTO> page, EamSparePartQuery query) {
        // 分页查询
        query.setSpareCode(StringUtils.isEmpty(query.getSpareCode()) ? query.getSpareCode() : query.getSpareCode() + "%");
        query.setSpareNmae(StringUtils.isEmpty(query.getSpareNmae()) ? query.getSpareNmae() : query.getSpareNmae() + "%");
        page = this.baseMapper.queryByPage(page, query);
        EamSparePartQuery queryPart;
        for( EamSparePartDTO item :page.getRecords()) {
            queryPart = new EamSparePartQuery();
            queryPart.setId(item.getId());
            queryPart.setDeleted(0);
            item.setDetails(this.getDetails(queryPart));
        }
        return page;
    }


    /**
     * 判断EamSparePart是已经存在
     *
     * @param dto
     * @return
     */
    public RetResult<String> isValid(EamSparePartDTO dto) {
        QueryWrapper<EamSparePart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", 0);
        queryWrapper.eq("spare_code", dto.getSpareCode());
        if (!StringUtils.isEmpty(dto.getId())) queryWrapper.ne("id", dto.getId());
        Integer count = this.count(queryWrapper);

        if (count > 0) return RetResult.fail("备件编号已存在");

        return RetResult.ok();
    }

    /**
     * 插入备件
     *
     * @param dto 插入对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void insert(EamSparePartDTO dto) {

        // 保存
        EamSparePart item = new EamSparePart();
        BeanUtils.copyProperties(dto, item);
        this.setDefaultValue(item);

        super.save(item);

        // 设置关联主键
        for (EamRefAssetPartDTO detail : dto.getDetails()) {
            detail.setSparePartId(item.getId());
        }

        // 保存明细表
        eamRefAssetPartService.saveBatch(dto.getDetails(),item.getId());
    }

    /**
     * 更新备件
     *
     * @param dto 更新对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void update(EamSparePartDTO dto) {

        // 保存
        EamSparePart item = new EamSparePart();
        BeanUtils.copyProperties(dto, item);

        // 默认数据
        this.setDefaultValue(item);

        super.updateById(item);

        // 设置关联主键
        for (EamRefAssetPartDTO detail : dto.getDetails()) {
            detail.setSparePartId(item.getId());
        }
        // 保存明细表
        eamRefAssetPartService.saveBatch(dto.getDetails(),item.getId());
    }

    /**
     * 根据批量主键删除备件(逻辑删除)
     *
     * @param dto
     * @return bool
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean logicDeleteList(EamSparePartDTO dto) {

        EamSparePart item = new EamSparePart();

        this.setDefaultValue(item);
        item.setVersion(dto.getVersion());
        item.setDeleted(1);

        QueryWrapper<EamSparePart> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", dto.getIds());

        return this.update(item, queryWrapper);
    }

    /**
     * 设置添加默认值
     *
     * @param item 添加对象
     */
    public void setDefaultValue(EamSparePart item) {
        if (StrUtil.isEmpty(item.getCreatedUserId())) {
            item.setCreatedUserId(SecurityUtils.getUserId());
            item.setCreateTime(new Date());
            item.setVersion(1L);
            item.setDeleted(0);
            item.setCreatedUserName(SecurityUtils.getUserName());
        }
        item.setLastUpdatedUserId(SecurityUtils.getUserId());
        item.setLastUpdateTime(new Date());

        item.setLastUpdatedUserName(SecurityUtils.getUserName());

    }

    /**
     * 根据主键获取备件信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public EamSparePartDTO get(String id) {
        // 查询数据
        EamSparePart item = this.baseMapper.selectById(id);
        EamSparePartDTO dto = new EamSparePartDTO();
        try {
            BeanUtils.copyProperties(item, dto);
        } catch (RuntimeException e) {
            throw e;
        }
        if(!StringUtils.isEmpty(dto.getMakerId())){
            EamMakerDTO eamMakerDTO = eamMakerService.get(dto.getMakerId());
            dto.setMakerCode(eamMakerDTO.getMakerCode());
            dto.setMakerName(eamMakerDTO.getMakerName());
        }
        if(!StringUtils.isEmpty(dto.getBrandId())){
            EamAssetBrandDTO eamAssetBrandDTO = eamAssetBrandService.get(dto.getBrandId());
            dto.setBrandCode(eamAssetBrandDTO.getBrandCode());
            dto.setBrandName(eamAssetBrandDTO.getMakerName());
        }
        if(!StringUtils.isEmpty(dto.getEquipTypeId())){
            EamAssetTypeDTO eamAssetTypeDTO = eamAssetTypeService.get(dto.getEquipTypeId());
            dto.setAssetTypeCode(eamAssetTypeDTO.getAssetTypeCode());
            dto.setAssetTypeName(eamAssetTypeDTO.getAssetTypeName());
        }

        if(!StringUtils.isEmpty(dto.getSpareKindId())){
            EquSparePartsTypeDTO equSparePartsTypeDTO = eamSparePartKindService.get(dto.getSpareKindId());
            dto.setSpareKindName(equSparePartsTypeDTO.getKindName());
        }

        EamSparePartQuery query = new EamSparePartQuery();
        query.setId(id);
        query.setDeleted(0);
        dto.setDetails(this.getDetails(query));
        return dto;
    }

    /**
     * 根据条件获取单表信息列表
     *
     * @param query 查询条件
     * @return List<EamSparePartDTO>
     */
    public List<EamSparePartDTO> getList(EamSparePartQuery query) {
        // 查询条件
        QueryWrapper<EamSparePart> qw = new QueryWrapper<>();
        qw.lambda().likeRight(!StrUtil.isEmpty(query.getSpareCode()), EamSparePart::getSpareCode, query.getSpareCode());
        qw.lambda().likeRight(!StrUtil.isEmpty(query.getSpareNmae()), EamSparePart::getSpareNmae, query.getSpareNmae());
        qw.lambda().eq(!StrUtil.isEmpty(query.getSpareType()), EamSparePart::getSpareType, query.getSpareType());
        qw.lambda().eq(!StrUtil.isEmpty(query.getEquipTypeId()), EamSparePart::getEquipTypeId, query.getEquipTypeId());
        qw.lambda().eq(query.getDeleted() != null, EamSparePart::getDeleted, query.getDeleted());
        qw.lambda().eq(query.getEnabled() != null, EamSparePart::getEnabled, query.getEnabled());
        qw.lambda().eq(query.getAssetKind() != null, EamSparePart::getAssetKind, query.getAssetKind());
        // 查询数据
        List<EamSparePart> list = super.list(qw);

        return convertList(list, EamSparePartDTO.class);
    }

    /**
     * 根据主键获取备件信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public void deleteItem(String id) {
        EamSparePart item = getById(id);
        this.setDefaultValue(item);
        item.setDeleted(1);

        this.updateById(item);
    }

    /**
     * 获取根据关联id详情表列表
     * @param query 查询条件
     * @return List<EamSparePartDTO>
     */
    public List<EamRefAssetPartDTO> getDetails(EamSparePartQuery query) {
        // 查询条件
        QueryWrapper<EamRefAssetPart> qw = new QueryWrapper<>();
        qw.lambda().eq(!StrUtil.isEmpty(query.getId()), EamRefAssetPart::getSparePartId, query.getId());
        qw.lambda().eq(query.getDeleted() != null, EamRefAssetPart::getDeleted, query.getDeleted());
        // 查询数据
        List<EamRefAssetPart> list = eamRefAssetPartService.list(qw);
        List<EamRefAssetPartDTO> listDTO = convertList(list, EamRefAssetPartDTO.class);
        for (EamRefAssetPartDTO item : listDTO){
            EamAssetInfoDTO eamAssetDTO = eamAssetInfoService.get(item.getAssetId());
            item.setAssetCode(eamAssetDTO.getAssetCode());
            item.setAssetName(eamAssetDTO.getAssetName());
        }
        return listDTO;
    }
}

